//
//  ExtensionVC.swift
//  swiftDemo
//
//  Created by yaqiang bi on 2017/3/30.
//  Copyright © 2017年 毕小强. All rights reserved.
//
//  延展

import UIKit

class ExtensionVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        let oneInch = 25.4.km
        print("\(oneInch) m")
        let threeFeet = 3.0.ft
        print("\(threeFeet) meters")
        
        3.repetitions {
            print("Hello world")
        }
        
        var someInt = 5
        someInt.square()
        print(someInt)
        someInt.repeatOutput { (arr) in
            print(arr)
        }
        
        print(123456789[8])
        
        func printInterKinds(_ numbers:[Int]){
            for number in numbers{
                switch number.kind {
                case .正:
                    print("+")
                case .零:
                    print("0")
                case .负:
                    print("-")
                }
            }
        }
        
        printInterKinds([2,3,0,-1,99,-89])
        // Do any additional setup after loading the view.
    }


    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}


extension Double{
    var km:Double {return self*1000.0}
    var m:Double {return self}
    var cm:Double {return self/100.0}
    var mm:Double {return self/1_000.0}
    var ft:Double {return self/3.28084}
    
}

struct Size {
    var width = 0.0, height = 0.0
}
struct Point {
    var x = 0.0, y = 0.0
}
struct Rect {
    var origin = Point()
    var size = Size()
}

extension Rect{
    init(center:Point,size:Size) {
        let X = center.x - size.width/2.0
        let Y = center.y - size.height/2.0
        self.init(origin: Point(x:X,y:Y), size: size)
    }
}

struct Personal {
    var name:String
    var age:Int
    init(_ name:String,age:Int) {
        self.name = "My name is" + name
        self.age = age
    }
}

extension Personal{
    init(stagename name:String) {
        self.init(name, age: 18)
    }
}


extension Int{
    func repetitions(tast:()->Void)  {
        for _ in 0...self {
            tast()
        }
    }
    
    func repeatOutput(output:([Int])->Void) {
        var arr = [Int]()
        for index in 0...self{
            arr.append(index)
        }
        output(arr)
    }
    
    mutating func square()  {
        self = self*self
    }
    
    subscript(digitIndex:Int)->Int{
        var decimalBase = 1
        
        for _ in 0..<digitIndex{
            decimalBase *= 10
        }
        return (self/decimalBase)%10
    }
    
    enum Kind {
        case 正,负,零
    }
    
    var kind:Kind{
        switch self {
        case 0:
           return .零
        case let x where x>0:
            return .正
        default:
            return .负
        }
    }
}
